package cn.edu.stu.syllabus.adapters;

import android.content.res.ColorStateList;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.CardView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.edu.stu.syllabus.R;
import cn.edu.stu.syllabus.application.SyllabusApplication;
import cn.edu.stu.syllabus.base.BaseQuickClickAdapter;
import cn.edu.stu.syllabus.repository.library.local.BookEntry;

/**
 * Created by daidaijie on 2017/3/27.
 */

public class BookAdapter extends BaseQuickClickAdapter<BookEntry, BookAdapter.ViewHolder> {

    private boolean mIsShowNum;

    @Override
    protected ViewHolder createBaseViewHolder(View view) {
        return new ViewHolder(view);
    }

    public BookAdapter(List<BookEntry> data) {
        this(data, true);
    }

    public BookAdapter(List<BookEntry> data, boolean isShowNum) {
        super(R.layout.item_book, data);
        mIsShowNum = isShowNum;
    }

    @Override
    protected void convert(final ViewHolder helper, final BookEntry item) {
        helper.mNameTextView.setText(item.getName());
        helper.mPressTextView.setText(item.getPress());
        helper.mPressDateTextView.setText(item.getPressDate());
        helper.mAuthorTextView.setText(item.getAuthor());

        int inLibraryNum = item.getInLibraryNum();
        int sumNum = item.getSumNum();

        if (mIsShowNum) {
            helper.mNumberTextView.setVisibility(View.VISIBLE);
            helper.mNumberTextView.setText(inLibraryNum + " / " + sumNum);
        } else {
            helper.mNumberTextView.setVisibility(View.GONE);
        }

        int colorHave = ContextCompat.getColor(SyllabusApplication.getContext(), R.color.material_green_400);
        int colorHaveNot = ContextCompat.getColor(SyllabusApplication.getContext(), R.color.material_red_300);

        if (inLibraryNum > 0) {
            helper.mNumberTextView.setTextColor(colorHave);
        } else {
            helper.mNumberTextView.setTextColor(colorHaveNot);
        }

        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
            ColorStateList colorStateList = ContextCompat.getColorStateList(
                    SyllabusApplication.getContext(), R.color.selected_card_press
            );
            helper.mBookCardView.setCardBackgroundColor(colorStateList);
            helper.mBookCardView.setForeground(null);
        }

        helper.mBookCardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null && isClickable()) {
                    mOnItemClickListener.onItemClick(item, helper.getAdapterPosition());
                }
            }
        });

        helper.mBookCardView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mOnItemLongClickListener != null && isClickable()) {
                    return mOnItemLongClickListener.onItemLongClick(item, helper.getAdapterPosition());
                }
                return false;
            }
        });
    }

    public void movePositionToTop(int position) {
        BookEntry bookEntry = getItem(position);
        remove(position);
        addData(0, bookEntry);
    }

    public class ViewHolder extends BaseViewHolder {

        @Nullable
        @BindView(R.id.bookCardView)
        CardView mBookCardView;
        @Nullable
        @BindView(R.id.pressTextView)
        TextView mPressTextView;
        @Nullable
        @BindView(R.id.pressDateTextView)
        TextView mPressDateTextView;
        @Nullable
        @BindView(R.id.nameTextView)
        TextView mNameTextView;
        @Nullable
        @BindView(R.id.authorTextView)
        TextView mAuthorTextView;
        @Nullable
        @BindView(R.id.numberTextView)
        TextView mNumberTextView;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
